package com.xingdata.jjxc.activity.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.VideoUploadActivity;
import com.xingdata.jjxc.adapter.LocalVideoAdapter;
import com.xingdata.jjxc.callback.DilalogListener;
import com.xingdata.jjxc.enty.LocalVideoEntity;
import com.xingdata.jjxc.utils.Consumer;
import com.xingdata.jjxc.utils.ImageUtils;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.VideoTools;
import com.xingdata.jjxc.views.MyAlertDialog;

public class LocalVideoFragment extends Fragment implements OnClickListener,
		OnItemClickListener {
	private GridView video_lv;
	private static List<LocalVideoEntity> localVideoEntities = new ArrayList<LocalVideoEntity>();
	private LocalVideoEntity entity;
	private LocalVideoAdapter localVideoAdp;
	private TextView localvideo_compile_iv;
	private String TAG = "LocalVideoFragment";
	private static Handler handler;
	private final static int reafresh = 0;
	private ArrayList<ImageView> imageViews = new ArrayList<ImageView>();
	private Consumer consumer=new Consumer();

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.localvideo, container, false);

		handler = new Handler() {
			public void handleMessage(android.os.Message msg) {

				switch (msg.what) {
				case reafresh:
					localVideoAdp.notifyDataSetChanged();
					break;

				default:
					break;
				}
			};
		};
		initView(view);
		return view;
	}

	private void initView(View view) {
		video_lv = (GridView) view.findViewById(R.id.video_lv);
		localvideo_compile_iv = (TextView) view
				.findViewById(R.id.localvideo_compile_iv);
		localvideo_compile_iv.setOnClickListener(this);
		localVideoAdp = new LocalVideoAdapter(this.getActivity(),
				localVideoEntities,imageViews,consumer);
		video_lv.setAdapter(localVideoAdp);
		video_lv.setOnItemClickListener(this);
		selectLocalVideo();
	}

	private void addvideo() {
		if (localVideoEntities.size() != 0) {
			localVideoEntities.clear();
		}
		//
		// entity = new LocalVideoEntity();
		// for (int i = 0; i < 10; i++) {
		// entity.setCirculationdate("15/11");
		// entity.setFlow("12mb");
		// mlist.add(entity);
		// }

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.localvideo_compile_iv:
			alertdialog();
			break;

		default:
			break;
		}
	}

	private void alertdialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("title");
		builder.setMessage("sssssssssss");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.show();
	}

	private static void selectLocalVideo() {
		File mfile = new File(SDCardTools.getFilePath(SDCardTools.VIDEO_NAME));

		if (localVideoEntities.size() != 0) {
			localVideoEntities.clear();
		}
		for (File file : mfile.listFiles()) {
			// 遍历删除文件
			LocalVideoEntity entity = new LocalVideoEntity();
			entity.setName(file.getName());
			entity.setUrl(file.getAbsolutePath());
			// float num = (float) file.length() / 1024 / 1024;
			// entity.setfSize((df.format(num) + "M"));
			entity.setfSize(StringUtil.formatSize(file.length() + ""));
			entity.setCrtTime(StringUtil.ToStringTime(new Date(file
					.lastModified())));
			entity.setCrtDate(StringUtil.ToStringData(new Date(file
					.lastModified())));
			localVideoEntities.add(entity);
		}
		Message message = new Message();
		message.what = reafresh;
		handler.sendMessage(message);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, final int position,
			long id) {

		ArrayList<String> titleList = new ArrayList<String>();
		titleList.add("");
		titleList.add("播放");
		titleList.add("分享");
		new MyAlertDialog(getActivity(), titleList, new DilalogListener() {
			@Override
			public void onSure() {
				VideoTools.palyVideo((Activity)getActivity(),
						localVideoEntities.get(position).getUrl());
			}

			@Override
			public void onCancel() {
				Bundle bundle = new Bundle();
				bundle.putString("url", localVideoEntities.get(position)
						.getUrl());
				Intent intent = new Intent(getActivity(),
						VideoUploadActivity.class);
				intent.putExtras(bundle);
				getActivity().startActivity(intent);
			}
		});

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		consumer.cancleAll();
		for (ImageView view : imageViews) {
			ImageUtils.releaseImageViewResouce(view);
		}
	}
}
